package cn.e2win.biz.sys;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import cn.e2win.base.dao.GenericDAOHibernateImpl;
import cn.e2win.base.entity.SysDict;
import cn.e2win.base.json.vo.Pager;


@Repository
public class SysDictDaoImpl extends GenericDAOHibernateImpl<SysDict, Integer> implements SysDictDao {
	@Override
	public Map<String, Object> initDict(){
		Map<String, Object> rMap= new HashMap<String, Object> ();
		List<Map<String, Object>> list = super.listedSql("SELECT myKey,myValue FROM sys_dict WHERE myType='ROOT'", null);// GROUP BY myKey
		//Integer editAbleInt = 1;
		Map<String, Object> rootMap = new HashMap<String,Object>();
		for(Map map : list){
			rootMap.put((String)map.get("myKey"), map.get("myValue"));
			rMap.putAll(getDictListAndMap((String)map.get("myKey")));
			/*if(editAbleInt.equals(map.get("editAbleInt"))){
				rMap.putAll()
			}else{
				rMap.put("LIST_" + map.get("myKey"), getDictList((String)map.get("myKey")));
			}*/
		}
		rMap.put("MAP_ROOT",rootMap);
		return rMap;		
	}

	
	@Override
	public Map<String,Object> getDictListAndMap(String type) {
		String listKey = "LIST_" + type;
		String mapKey = "MAP_" + type;
		Map<String, Object> map = new HashMap<String, Object>();
		long t1 = System.currentTimeMillis();
		String sql = "SELECT * FROM sys_dict WHERE myType='" + type + "' ORDER BY coId,orderIndex";
		
		List<Map<String, Object>> list = super.listedSql(sql, null);
		List<SimpleMap> defultList = new ArrayList<SimpleMap>();
		List<SimpleMap> rList = new ArrayList<SimpleMap>();
		Map<String,Object> rMap = new HashMap<String, Object>();
		Integer coId = null;
		for(Map<String, Object> item : list){
			if(item.get("myKey")!=null){
				rMap.put(item.get("myKey").toString(), item.get("myValue"));
				SimpleMap simpleMap = new SimpleMap();
				simpleMap.setKEY(item.get("myKey")+"");
				simpleMap.setVALUE(item.get("myValue")+"");
				if(item.get("coId")==null){
					defultList.add(simpleMap);
				}else{
					if(item.get("coId").equals(coId)){
						
					}else{
						
							map.put(listKey + (coId==null?"":"_" + coId), rList);	
							System.err.println(listKey + (coId==null?"":"_" + coId) + "====" + rList.size());
							
							coId = (Integer) item.get("coId");
							rList = new ArrayList<SimpleMap>();
							rList.addAll(defultList);

						
					}
					rList.add(simpleMap);
				}
				
			}
			if(null != item.get("script")){
				List<Map<String, Object>> scriptList = super.getListBySqlAutoParam((String)item.get("script"),new HashMap<String, Object>());

				for(Map<String, Object> scriptItem : scriptList){

					if(scriptItem.get("myKey")!=null){
						rMap.put(scriptItem.get("myKey").toString(), scriptItem.get("myValue"));
						SimpleMap simpleMap = new SimpleMap();
						simpleMap.setKEY(scriptItem.get("myKey")+"");
						simpleMap.setVALUE(scriptItem.get("myValue")+"");
						if(scriptItem.get("coId")==null){
							defultList.add(simpleMap);
						}else{
							if(scriptItem.get("coId").equals(coId)){
								
							}else{
								
									map.put(listKey + (coId==null?"":"_" + coId), rList);	
									System.err.println(listKey + (coId==null?"":"_" + coId) + "====" + rList.size());
									
									coId = (Integer) scriptItem.get("coId");
									rList = new ArrayList<SimpleMap>();
									rList.addAll(defultList);
	
								
							}
							rList.add(simpleMap);
						}
						
					}
				}
			}
		}
		map.put(mapKey, rMap);
		map.put(listKey + (coId==null?"":"_" + coId), rList);	
		map.put(listKey, defultList);
		long t2 = System.currentTimeMillis();
		//System.err.println("====" + (t2-t1)+ "====" + list.size() + "===" + sql);
		return map;
	}
	
	
	
	/*
	
	
	@Override
	public Map<String,List<SimpleMap>> getDictList(String type) {
		String key = "LIST_" + type;
		Map<String, List<SimpleMap>> map = new HashMap<String, List<SimpleMap>>();
		long t1 = System.currentTimeMillis();
		String sql = "SELECT * FROM sys_dict WHERE myType='" + type + "' ORDER BY coId";
		
		List<Map<String, Object>> list = super.listedSql(sql, null);
		List<SimpleMap> defultList = new ArrayList<SimpleMap>();
		List<SimpleMap> rList = new ArrayList<SimpleMap>();
		Integer coId = null;
		for(Map<String, Object> item : list){
			if(item.get("myKey")!=null){
				SimpleMap simpleMap = new SimpleMap();
				simpleMap.setKEY(item.get("myKey")+"");
				simpleMap.setVALUE(item.get("myValue")+"");
				if(item.get("coId")==null){
					defultList.add(simpleMap);
				}else{
					if(item.get("coId").equals(coId)){
						
					}else{
						
							map.put(key + (coId==null?"":"_" + coId), rList);	
							System.err.println(key + (coId==null?"":"_" + coId) + "====" + rList.size());
							
							coId = (Integer) item.get("coId");
							rList = new ArrayList<SimpleMap>();
							rList.addAll(defultList);

						
					}
					rList.add(simpleMap);
				}
				
			}
			if(null != item.get("script")){
				rList.addAll(getDictListBySql((String)item.get("script")));
			}
		}
		map.put(key + (coId==null?"":"_" + coId), rList);	
		System.err.println(key + (coId==null?"":"_" + coId) + "====" + rList.size());
		map.put(key, defultList);
		System.err.println(key + "====" + defultList.size());
		long t2 = System.currentTimeMillis();
		System.err.println("====" + (t2-t1)+ "====" + list.size() + "===" + sql);
		return map;
	}
*/
/*
	@Override
	public List<SimpleMap> getDictList(String type) {
		long t1 = System.currentTimeMillis();
		String sql = "SELECT * FROM sys_dict WHERE myType='" + type + "'";
		List<SimpleMap> list = getDictListBySql(sql);
		long t2 = System.currentTimeMillis();
		System.err.println("====" + (t2-t1)+ "====" + list.size() + "===" + sql);
		return list;
	}
*/

	
	private List<SimpleMap> getDictListBySql(String sql) {
		List<Map<String, Object>> list = super.listedSql(sql, null);
		List<SimpleMap> rList = new ArrayList<SimpleMap>();
		for(Map<String, Object> map : list){
			if(map.get("myKey")!=null){
				SimpleMap simpleMap = new SimpleMap();
				simpleMap.setKEY(map.get("myKey")+"");
				simpleMap.setVALUE(map.get("myValue")+"");
				rList.add(simpleMap);
			}
			if(null != map.get("script")){
				rList.addAll(getDictListBySql((String)map.get("script")));
			}
		}		
		return rList;
	}
	
	@Override
	public Map<Object, List> getCompanyUserList(boolean activeOnly) {

		Map<Object, List> rMap= new HashMap<Object, List> ();
		String sql = "SELECT coId, id AS myKey,userName myValue FROM sys_user WHERE 1=1 ORDER BY coId, userName";
		if(activeOnly){
			sql = "SELECT coId, id AS myKey,userName myValue FROM sys_user WHERE 1=1 AND activeStatusInt=1 ORDER BY coId, userName";
		}
		List<Map<String, Object>> list = super.listedSql(sql, null);
		for(Map map : list){
			if(rMap.containsKey(map.get("coId"))){
			}else{
				rMap.put(map.get("coId"), new ArrayList());
			}
			rMap.get(map.get("coId")).add(map);
		}
		return rMap;	
	}
	
	

}
